<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>字典目录管理</title>
    <#include "/common/link.ftl">

    <link rel="stylesheet" href="/static/js/plugins/bootstrap-treeview/bootstrap-treeview.min.css">
    <script src="/static/js/plugins/bootstrap-treeview/bootstrap-treeview.min.js"></script>

    <script>
        $(function () {
            var tree;
            $.get("/systemDictionary/treeData", function (data) {
                tree = $('#treeview-searchable').treeview({
                    color: "#428bca",
                    // 给数据加一个目录
                    data: [{text: "数据字典目录", nodes: data}],
                    // 节点被选中事件
                    onNodeSelected: function (event, node) {
                        // 避免选中目录栏报错
                        if (node.id === undefined) {
                            location.href = "/systemDictionary/list?nodeId=" + node.nodeId;
                        }else{
                            //node.id :数据字典id, nodeId :节点id
                            location.href = "/systemDictionary/list?parentId=" + node.id + "&nodeId=" + node.nodeId;
                        }
                    }

                });
                // 选择给定的节点
                // 选中节点回显   方法名           节点id        默认参数
                tree.treeview('selectNode', [${qo.nodeId}, {silent: true}]);
                // 自己展开
                tree.treeview('expandNode', [${qo.nodeId}, {silent: true}]);

                if (`${qo.nodeId}` != 0) {
                    //当前节点
                    var node = tree.treeview('getNode', ${qo.nodeId});

                    // 展开父节点
                    while (node.parentId) {
                        var parent = tree.treeview('getParent', node); // 获取当前节点的父节点
                        tree.treeview('expandNode', [ parent, { silent: true }]); // 展开父节点
                        node = tree.treeview('getNode', node.parentId); // 把当前节点的父节点 赋值给节点
                    }

                }
            });
            // 添加/编辑按钮
            $('.btn-input').click(function () {

                $('#editForm').clearForm(true); //清空表单数据
                //当前节点
                var node = tree.treeview('getNode', ${qo.nodeId});
                $('#editForm input[name=parentName]').val(node.text); // 父目录
                $('#editForm input[name=parentId]').val(node.id); // 父id

                // 数据回显
                var json = $(this).data('json');
                if (json){
                    $('#editForm input[name=id]').val(json.id); // id
                    $('#editForm input[name=name]').val(json.name); // 名称
                    $('#editForm input[name=sn]').val(json.sn); //编码
                    $('#editForm input[name=intro]').val(json.intro); // 简介
                }
                $('#editModal').modal('show');

            });

            // 添加/编辑按钮 异步提交
            // 给保存按钮一个点击事件
            $('.btn-submit').click(function () {
                // form表单 异步提交
                $('#editForm').ajaxSubmit(function (data) {
                    if (data.success) {
                        window.location.reload(); // 成功后重新加载
                    }
                });

            });

        });

    </script>

</head>
<body class="hold-transition skin-black sidebar-mini">
<div class="wrapper">
    <#include "/common/navbar.ftl">

    <#assign currentMenu="systemDictionary"/>

    <#include "/common/menu.ftl">
    <div class="content-wrapper">
        <section class="content-header">
            <h1>数据字典管理</h1>
        </section>
        <section class="content">
            <div class="box">
                <div class="row" style="margin:20px">
                    <#-- 定义树结构的位置 占1/3位置-->
                    <div class="col-xs-4">
                        <div id="treeview-searchable" class="margin-left:30px;"></div>
                    </div>
                    <div class="col-xs-8">
                        <!--高级查询--->
                        <form class="form-inline" id="searchForm" action="/systemDictionary/list" method="post">
                            <input type="hidden" name="currentPage" id="currentPage" value="1">
                            <#-- 分页需要带上这两个id-->
                            <input type="hidden" name="parentId"  value="${qo.parentId}">
                            <input type="hidden" name="nodeId"  value="${qo.nodeId}">
                            <a href="#" class="btn btn-success btn-input" style="margin: 10px">
                                <span class="glyphicon glyphicon-plus"></span> 添加
                            </a>
                        </form>
                        <!--编写内容-->
                        <div class="box-body table-responsive no-padding ">
                            <table class="table table-hover table-bordered table-striped">
                                <thead>
                                <tr>
                                    <th>编号</th>
                                    <th>数据名称</th>
                                    <th>数据编码</th>
                                    <th>字典目录</th>
                                    <th>操作</th>
                                </tr>
                                </thead>
                                <tbody>
                                <#list pageInfo.list as s>
                                    <tr>
                                        <td>${s.id}</td>
                                        <td>${s.name}</td>
                                        <td>${s.sn}</td>
                                        <td>${s.parentName}</td>
                                        <td>
                                            <a href="#" class="btn btn-info btn-xs btn-input" data-json='${s.toJson()}'>
                                                <span class="glyphicon glyphicon-pencil"></span> 编辑
                                            </a>
                                            <a href="javascript:;" class="btn btn-danger btn-xs btn-delete"
                                               data-url="/systemDictionary/delete?id=${s.id}">
                                                <span class="glyphicon glyphicon-pencil"></span> 删除
                                            </a>
                                        </td>
                                    </tr>
                                </#list>

                                </tbody>
                            </table>
                            <#-- 分页-->
                            <#include "/common/page.ftl" >
                        </div>
                    </div>
                </div>

            </div>
        </section>
    </div>
</div>


<!-- Modal -->
<div class="modal fade" id="editModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span>
                </button>
                <h4 class="modal-title" id="myModalLabel">新增/编辑</h4>
            </div>
            <form class="form-horizontal" action="/systemDictionary/saveOrUpdate" method="post" id="editForm">
                <div class="modal-body">
                    <input type="hidden" name="parentId">
                    <input type="hidden" name="id">
                    <div class="form-group" style="margin-top: 10px;">
                        <label for="name" class="col-sm-3 control-label">字典目录：</label>
                        <div class="col-sm-6">
                            <input type="text" class="form-control" name="parentName" readonly>
                        </div>
                    </div>
                    <div class="form-group" style="margin-top: 10px;">
                        <label for="name" class="col-sm-3 control-label">名称：</label>
                        <div class="col-sm-6">
                            <input type="text" class="form-control" name="name"
                                   placeholder="请输入字典目录标题">
                        </div>
                    </div>
                    <div class="form-group" style="margin-top: 10px;">
                        <label for="sn" class="col-sm-3 control-label">编码：</label>
                        <div class="col-sm-6">
                            <input type="text" class="form-control" name="sn"
                                   placeholder="请输入字典目录编码">
                        </div>
                    </div>
                    <div class="form-group" style="margin-top: 10px;">
                        <label for="sn" class="col-sm-3 control-label">简介：</label>
                        <div class="col-sm-6">
                            <input type="text" class="form-control" name="intro"
                                   placeholder="请输入字典目录简介">
                        </div>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                    <button type="button" class="btn btn-primary btn-submit">保存</button>
                </div>
            </form>
        </div>
    </div>
</div>

</body>
</html>
